package client

import (
	"context"
	"github.com/golang/protobuf/ptypes/empty"
	"github.com/micro/go-micro/v2/client"
	"github.com/micro/go-micro/v2/client/grpc"

	"github.com/golang/protobuf/ptypes/wrappers"
	"kop/framework/wrapper"
	"kop/pb"
	"kop/pb/service"
)

var GuildService service.GuildService

func init() {

	var c = grpc.NewClient(client.Wrap(wrapper.ClientLogWrapper))
	_ = c.Init()
	//s.Init(test.Flags()...)

	GuildService = service.NewGuildService("go.micro.srv.guild", c)
}

func GetGuilds(serverID, page int32) (*pb.Guilds, error) {

	return GuildService.GetGuilds(context.Background(), &service.ServerPageRequest{ServerID: serverID, Page: page})
}

//func RandGuilds(serverID int32) ([]*pb.Guild, error) {
//
//	var resp, err = GuildService.RandGuilds(context.Background(), &wrappers.Int32Value{Value: serverID})
//	if err != nil {
//		return nil, err
//	}
//
//	return resp.Guilds, nil
//}

func RandJoin(roleID, serverID int32) (bool, error) {

	var resp, err = GuildService.RandJoin(context.Background(), &service.DefaultRequest{RoleID: roleID, Value: serverID})
	if err == nil {
		return resp.Value, nil
	} else {
		return false, err
	}
}

func GetGuild(serverID, guildID int32) (*pb.Guild, error) {

	return GuildService.GetGuild(context.Background(), &service.GuildRequest{ServerID: serverID, GuildID: guildID})
}

func MyGuild(roleID, serverID, guildID int32) (*pb.Guild, error) {

	var guild, err = GuildService.MyGuild(context.Background(), &service.GuildRequest{RoleID: roleID, ServerID: serverID, GuildID: guildID})
	if err == nil && guild.GuildID == 0 {
		guild = nil
	}

	return guild, err
}

func GetMember(roleID, serverID, guildID int32) (*pb.GuildMember, error) {

	return GuildService.GetMember(context.Background(), &service.GuildRequest{RoleID: roleID, ServerID: serverID, GuildID: guildID})
}

func Members(serverID, guildID int32) ([]*pb.GuildMember, error) {

	var resp, err = GuildService.Members(context.Background(), &service.GuildRequest{ServerID: serverID, GuildID: guildID})
	if err != nil {
		return nil, err
	}

	return resp.GuildMembers, nil
}

func Create(role *pb.Role, request *pb.GuildCreateRequest) (*pb.Guild, error) {

	return GuildService.Create(context.Background(), &service.GuildCreateRequest{Role: role, Request: request})
}

func Join(role *pb.Role, guildID int32) (*pb.Guild, error) {

	return GuildService.Join(context.Background(), &service.GuildJoinRequest{Role: role, GuildID: guildID})
}

//func Build(roleID, serverID, guildID int32, buildType pb.GuildBuildType) (*pb.Guild, error) {
//
//	return GuildService.Build(context.Background(), &service.GuildBuildRequest{BuildType: buildType})
//}

func Apply(role *pb.Role, guildID int32) (*empty.Empty, error) {

	return GuildService.Apply(context.Background(), &service.GuildJoinRequest{Role: role, GuildID: guildID})
}

func GetAppliesByID(IDs []int32) ([]*pb.Role, error) {

	var resp, err = GuildService.GetAppliesByID(context.Background(), &pb.Int32Slice{Slices: IDs})
	if err != nil {
		return nil, err
	}

	return resp.Roles, nil
}

// 删除入会申请
// applyID: 申请ID
func DelApply(operator, serverID, guildID int32, applyIDs []int32) (*empty.Empty, error) {

	return GuildService.DelApply(context.Background(), &service.GuildDelApplyRequest{GuildRequest: &service.GuildRequest{RoleID: operator, ServerID: serverID, GuildID: guildID}, IDs: applyIDs})
}
func Online(roleID, serverID, guildID int32) (*empty.Empty, error) {

	return GuildService.Online(context.Background(), &service.GuildRequest{RoleID: roleID, ServerID: serverID, GuildID: guildID})
}
func Offline(roleID, serverID, guildID int32) (*empty.Empty, error) {

	if guildID <= 0 {
		return nil, nil
	}

	return GuildService.Offline(context.Background(), &service.GuildRequest{RoleID: roleID, ServerID: serverID, GuildID: guildID})
}

//func BuildConfigs() ([]*pb.GuildBuildConf, error) {
//
//	var resp, err = GuildService.BuildConfigs(context.Background(), &empty.Empty{})
//	if err != nil {
//		return nil, err
//	}
//
//	return resp.BuildConfigs, nil
//}

func GetNotice(guildID, serverID int32) string {

	var resp, err = GuildService.GetNotice(context.TODO(), &service.GuildRequest{GuildID: guildID, ServerID: serverID})
	if err != nil {
		panic(err)
	}
	return resp.Value
}

func GetSkills(roleID int32) []*pb.GuildSkill {

	var resp, err = GuildService.GetSkills2(context.Background(), &wrappers.Int32Value{Value: roleID})
	if err != nil {
		panic(err)
	}

	if resp.Skills == nil {
		resp.Skills = []*pb.GuildSkill{}
	}

	return resp.Skills
}
